package com.cloud.scheduled;

import com.cloud.dao.city.CityDao;
import com.cloud.dao.cityweather.CityWeatherDao;
import com.cloud.model.entity.city.City;
import com.cloud.service.cityweather.CityWeatherService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;

import java.util.List;

/**
 * 更新天气信息
 */
@Component
@EnableScheduling
public class WeatherScheduled {

    private static final Logger LOG = LoggerFactory.getLogger(WeatherScheduled.class);

    @Autowired
    private CityDao cityDao;

    @Autowired
    private CityWeatherService cityWeatherService;
    /**
     * 每小时执行一次
     */
    @Scheduled(cron = "0 0 * * * ?")
    public void UpdateWeatherScheduled() {
        LOG.info("开始更新和风天气api信息");
        List<City> cities = cityDao.findAll();
        if (ObjectUtils.isEmpty(cities) || cities.size() == 0) {
            LOG.info("城市表没数据！定时器关闭");
            return;
        }
        cities.forEach(city -> {
            cityWeatherService.updateCityWeatherEntiy(city);
        });
    }
}
